﻿using DHQGHN.DAL.Base;
using DHQGHN.DAL.Common;
using DHQGHN.DAL.Models.DHQG;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



namespace DHQGHN.DAL.DAO.DHQG
{
    public class NguoiDungDAO : SqlDAO
    {
        public const string PROCEDURE_GET_LIST_PAGING = "spNguoiDung_GetListPaging";
        public const string PROCEDURE_INSERT_UPDATE = "spNguoiDung_InsertUpdate";
        public const string PROCEDURE_DELETE = "spNguoiDung_Delete";
        public const string PROCEDURE_GET_BY_ID = "spNguoiDung_GetById";
        public const string PROCEDURE_GET_USER_PM2 = "spNguoiDung_Get_PM2";
        public const string PROCEDURE_GET_USER_BY_DON_VI_ID = "spNguoiDungChiTieu_GetUseByDonViId";

        public const string PROCEDURE_GET_LIST_CHITIEU = "spNguoiDung_Get_List_ChiTieu";

        public const string PARAM_ID = "@NGUOI_DUNG_ID";
        public const string PARAM_HO_VA_TEN = "@HO_VA_TEN";
        public const string PARAM_TAI_KHOAN = "@TAI_KHOAN";
        public const string PARAM_DIEN_THOAI = "@DIEN_THOAI";
        public const string PARAM_DELETED = "@DELETED";
        public const string PARAM_IS_ADMIN = "@IS_ADMIN";
        public const string PARAM_PAGE_INDEX = "@PAGE_INDEX";
        public const string PARAM_PAGE_SIZE = "@PAGE_SIZE";
        public const string PARAM_LAST_UPDATED = "@LAST_UPDATED"; 
        public const string PARAM_DON_VI_ID = "@DON_VI_ID";
        public const string PARAM_IS_FIX = "@IS_FIX";

        public NguoiDungDAO(string connectionString, System.Type type):
            base(connectionString, type) { ;}

        public DataPage<NguoiDung> GetListPaging(NguoiDung model, int pageIndex, int pageSize, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_LIST_PAGING);
                cmd.Parameters.Add(CreateParameter(PARAM_HO_VA_TEN, model.HO_VA_TEN));
                cmd.Parameters.Add(CreateParameter(PARAM_TAI_KHOAN, model.TAI_KHOAN));
                cmd.Parameters.Add(CreateParameter(PARAM_DIEN_THOAI, model.DIEN_THOAI));
                cmd.Parameters.Add(CreateParameter(PARAM_DELETED, model.DELETED));
                cmd.Parameters.Add(CreateParameter(PARAM_IS_ADMIN, model.IS_ADMIN));
                cmd.Parameters.Add(CreateParameter(PARAM_DON_VI_ID, model.DON_VI_ID));
                cmd.Parameters.Add(CreateParameter(PARAM_PAGE_INDEX, pageIndex));
                cmd.Parameters.Add(CreateParameter(PARAM_PAGE_SIZE, pageSize));

                return GetDataPage<NguoiDung>(cmd, pageIndex, pageSize);
            }
            catch
            {
                throw;
            }
        }

        public NguoiDung GetById(int nguoiDungId, Dictionary<string,string> mapDictionary)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_BY_ID);
                cmd.Parameters.Add(CreateParameter(PARAM_ID, nguoiDungId));

                return GetSingle<NguoiDung>(cmd, mapDictionary);
            }
            catch
            {
                
                throw;
            }
        }

        public bool Save(NguoiDung model)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_INSERT_UPDATE);
                cmd.Parameters.Add(new SqlParameter(PARAM_ID, model.NGUOI_DUNG_ID));
                cmd.Parameters.Add(CreateParameter(PARAM_HO_VA_TEN, model.HO_VA_TEN));
                cmd.Parameters.Add(CreateParameter(PARAM_TAI_KHOAN, model.TAI_KHOAN));
                cmd.Parameters.Add(CreateParameter(PARAM_DIEN_THOAI, model.DIEN_THOAI));
                cmd.Parameters.Add(CreateParameter(PARAM_DON_VI_ID, model.DON_VI_ID));
                cmd.Parameters.Add(CreateParameter(PARAM_IS_ADMIN, model.IS_ADMIN));
                

                int result = ExecuteNonQuery(cmd);

                return result > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }

        public bool Delete(int[] nguoiDungId)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_DELETE);
                System.Data.DataTable tableId = GetTableId<int>(nguoiDungId);
                cmd.Parameters.Add(new SqlParameter("@TableId", tableId));

                int result = ExecuteNonQuery(cmd);
                return result > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }

        public bool Delete(int nguoiDungId)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_DELETE);
                cmd.Parameters.Add(new SqlParameter(PARAM_ID, nguoiDungId));

                int result = ExecuteNonQuery(cmd);
                return result > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }

        public NguoiDung GetUserInforFromPM2(string UserName, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_USER_PM2);
                cmd.Parameters.Add(CreateParameter(PARAM_TAI_KHOAN, UserName));

                return GetSingle<NguoiDung>(cmd, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        public List<NguoiDung> GetListUserByDonViId(int donViId, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_USER_BY_DON_VI_ID);
                cmd.Parameters.Add(CreateParameter(PARAM_DON_VI_ID, donViId));

                return GetList<NguoiDung>(cmd, mapDictionary);
            }
            catch 
            {                
                throw;
            }
        }

        public bool UpdateQuyenChot(int nguoiDungId, int isFix)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand("spNguoiDung_CapNhatQuyenChot");
                cmd.Parameters.Add(CreateParameter(PARAM_ID, nguoiDungId));
                cmd.Parameters.Add(CreateParameter(PARAM_IS_FIX, isFix));

                int result = ExecuteNonQuery(cmd);

                return result > 0 ? true : false;
            }
            catch 
            {                
                throw;
            }
        }

        public bool Reactive(int nguoiDungId)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand("spNguoiDung_Active");
                cmd.Parameters.Add(CreateParameter(PARAM_ID, nguoiDungId));
                int result = ExecuteNonQuery(cmd);

                return result > 0 ? true : false;
            }
            catch
            {
                
                throw;
            }
        }

        //public List<ChiTieu> GetListChiTieu(string UserName, Dictionary<string, string> mapDictionary)
        //{
        //    try
        //    {
        //        SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_LIST_CHITIEU);
        //        cmd.Parameters.Add(new SqlParameter(PARAM_TAI_KHOAN, UserName));

        //        return GetList<ChiTieu>(cmd, mapDictionary);
        //    }
        //    catch
        //    {
        //        throw;
        //    }
        //}
    }
}
